package com.xl.utils.util.设计模式.build模式;

/**
 * @author xionglin
 * @date 2021/6/3 21:45
 * @Description 人
 */

/**
 * 多个属性比如好几十个属性，就会有非常多的构成函数，在new对象的时候采用Builde来创建，可以使用lombok的@Builder注解
 * 下面是自己写的时间，lombok其实也是一样的
 */
public class People {
    private String name;
    private Integer age;
    //可能会有非常的多的属性，使用传统的构造器重载是比较复杂的
    private String address;
    private String tel;
    //全参构造
    People(String name,Integer age,String address,String tel){
        this.name = name;
        this.age = age;
        this.address = address;
        this.tel = tel;
    }
    People(String name,String address,String tel){
        this.name = name;
        this.address = address;
        this.tel = tel;
    }
    public static People.PeopleBuilder builder(){
        return new People.PeopleBuilder();
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                ", tel='" + tel + '\'' +
                '}';
    }

    //静态类做一个构造对象，实现链式调用
    public static class PeopleBuilder{
        PeopleBuilder(){
        }
        private String name;
        private Integer age;
        private String address;
        private String tel;

        public People.PeopleBuilder name(String name){
            this.name = name;
            return this;
        }
        public People.PeopleBuilder age(Integer age){
            this.age = age;
            return this;
        }
        public People.PeopleBuilder address(String address){
            this.address = address;
            return this;
        }
        public People.PeopleBuilder tel(String tel){
            this.tel = tel;
            return this;
        }
        public People build(){
            return new People(this.name,this.age,this.address,this.tel);
        }
    }
}
